
namespace Behemoth.SharePoint.Utilities {

    using System;
    using System.Runtime.InteropServices;
    using System.IO;

    using Microsoft.SharePoint;

    using Behemoth.Configuration;
    using Behemoth.SharePoint;
    using Behemoth.SharePoint.Logging;

    public partial class SiteProvisioningReceiver {

        #region Logging Code Pattern v2.0

        ////private ULSTraceLogging logWriter;

        protected string LOGGING_PRODUCT { get { return "Behemoth"; } }
        protected string LOGGING_CATEGORY { get { return "Provisioning"; } }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        public event LoggingEventHandler Logging;

        /// <summary>
        /// Put a method like this in your class that produces log entries
        /// </summary>
        /// <param name="e"></param>
        public void Log(LoggingEventArgs e) {
            // if nobody could be bothered to attach their own events, we should
            // still log things in the default way
            if (Logging == null)
                Logging += new LoggingEventHandler(ULSTraceLogging.Default.Log);
            if (Logging != null)
                Logging(this, e);
        }
        public void Log(string msg) {
            Log(msg, TraceSeverity.InformationEvent);
        }
        public void Log(string msg, TraceSeverity severity) {
            LoggingEventArgs e = new LoggingEventArgs(LOGGING_CATEGORY, LOGGING_PRODUCT, msg, severity);
            Log(e);
        }
        public void Log(Exception ex) {
            LoggingEventArgs e = new LoggingEventArgs(LOGGING_CATEGORY, LOGGING_PRODUCT, ex);
            Log(e);
        }

        #endregion

        private ListPropertyRepairFeatureProperties props;
        public ListPropertyRepairFeatureProperties TypedProperties {
            get { return props; }
        }

        private void EnsureProperties(SPFeatureReceiverProperties properties) {
            Log("Invoking EnsureProperties.");
            if (props == null)
                props = new ListPropertyRepairFeatureProperties(properties);
            else if (props.InitStatus != Status.Initialized)
                throw new Exception("property reader is not initialized. Can't continue.");
            Log("Leaving EnsureProperties.");
        }

        public virtual void OnActivated(SPFeatureReceiverProperties properties) {
            EnsureProperties(properties);
            OnActivated();
        }

        public void OnActivated() {
            SPWeb web = this.TypedProperties.Web;
            foreach (string listName in this.TypedProperties.ListFeatures) {
                // TODO does this get all features (what we want), or just activated features??
                SPFeature feature = web.Features.GetFeature(listName);
                if (feature != null)
                    Customization.FixCustomPropertyOnListSchema(web, feature, listName);
            }
        }

    } // class
} // namespace
